package org.jeecg.modules.product.basics.service.impl;

import org.jeecg.modules.product.basics.entity.TbBasicsStash;
import org.jeecg.modules.product.basics.entity.TbBasicsStashProduct;
import org.jeecg.modules.product.basics.mapper.TbBasicsStashMapper;
import org.jeecg.modules.product.basics.mapper.TbBasicsStashProductMapper;
import org.jeecg.modules.product.basics.service.ITbBasicsStashService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 仓库信息
 * @Author: jeecg-boot
 * @Date:   2024-07-05
 * @Version: V1.0
 */
@Service
public class TbBasicsStashServiceImpl extends ServiceImpl<TbBasicsStashMapper, TbBasicsStash> implements ITbBasicsStashService {

	@Autowired
	private TbBasicsStashMapper tbBasicsStashMapper;
	@Autowired
	private TbBasicsStashProductMapper tbBasicsStashProductMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(TbBasicsStash tbBasicsStash, List<TbBasicsStashProduct> tbBasicsStashProductList) {
		tbBasicsStashMapper.insert(tbBasicsStash);
		if(tbBasicsStashProductList!=null && tbBasicsStashProductList.size()>0) {
			for(TbBasicsStashProduct entity:tbBasicsStashProductList) {
				//外键设置
				entity.setStashId(tbBasicsStash.getId());
				tbBasicsStashProductMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(TbBasicsStash tbBasicsStash,List<TbBasicsStashProduct> tbBasicsStashProductList) {
		tbBasicsStashMapper.updateById(tbBasicsStash);
		
		//1.先删除子表数据
		tbBasicsStashProductMapper.deleteByMainId(tbBasicsStash.getId());
		
		//2.子表数据重新插入
		if(tbBasicsStashProductList!=null && tbBasicsStashProductList.size()>0) {
			for(TbBasicsStashProduct entity:tbBasicsStashProductList) {
				//外键设置
				entity.setStashId(tbBasicsStash.getId());
				tbBasicsStashProductMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		tbBasicsStashProductMapper.deleteByMainId(id);
		tbBasicsStashMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			tbBasicsStashProductMapper.deleteByMainId(id.toString());
			tbBasicsStashMapper.deleteById(id);
		}
	}
	
}
